package com.ruoyi.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.DemoTest;
import com.ruoyi.web.domain.dto.ArticleRequestDTO;
import com.ruoyi.web.enums.ArticleTypeEnum;
import com.ruoyi.web.enums.CountEnum;
import com.ruoyi.web.mapper.DemoTestMapper;
import com.ruoyi.web.service.IDemoTestService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;

/**
 * demo测试Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-08
 */
@Service
public class DemoTestServiceImpl extends ServiceImpl<DemoTestMapper, DemoTest> implements IDemoTestService {

    @Autowired
    private DemoTestMapper demoTestMapper;

    /**
     * 查询demo测试列表
     *
     * @param demoTest demo测试
     * @return demo测试
     */
    @Override
    public List<DemoTest> selectDemoTestList(DemoTest demoTest)
    {
        List<DemoTest> demoTestList = demoTestMapper.selectList(buildQueryWrapper(demoTest));
        return demoTestList;
    }


    private LambdaQueryWrapper<DemoTest> buildQueryWrapper(DemoTest query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<DemoTest> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(query.getTestAge()), DemoTest::getTestAge, query.getTestAge());
        lqw.like(StringUtils.isNotBlank(query.getTestName()), DemoTest::getTestName, query.getTestName());
        lqw.orderByDesc(DemoTest::getCreateTime);
        return lqw;
    }

    /**
     * 查询demo模块测试
     *
     * @param id demo模块测试主键
     * @return demo模块测试
     */
    @Override
    public DemoTest selectDemoTestById(Long id)
    {
        return demoTestMapper.selectDemoTestById(id);
    }

    @Override
    public AjaxResult publishAnArticle(ArticleRequestDTO dto) {
        //不同类型  执行不同的impl
        dto.getArticleType().getEnumStrategyService().doSomeThing(dto);
        return AjaxResult.success("发表成功");
    }

    @Override
    public AjaxResult countStrategy(ArticleRequestDTO dto) {
        //不同类型  执行不同的计算方法
        int count = dto.getCountEnum().Count(10, 10);
        return AjaxResult.success("计算结果为["+count+"]");
    }

}
